		////////// 		 Name: CursorMenuLoop
		////////// written by: Marcin
		/////////      INPUTS:  r4 - > mask of which rows are available to move a cursor on ( MSB -> LSB )  Row5, Row4, Row3, Row2, Row1, Row0(never row0)
		/////////			          
		/////////	  OUTPUTS:  r2 - > returns which menu was pressed (decimal)( (never)1, 2, 3, 4, 5,6 ) //if 99, means back was pressed
		/////////
		/////////    Description:   What this subroutine does is take a mask that dictates where it can scroll, and update the cursor
		/////////					as the user presses buttons.  When the user finally selects a button, it returns what menu he pressed.
				
				
				
				//////////////////ERRRATA
				////////////////// Masking function does not do what I wanted it to. need to think of a cool way to get around this.
		CursorMenuLoop:
						
				st	r0, sp, 0	//scratch
				st	r1, sp, 1	//scratch
				st	r2, sp, 2		//outputvar which menu was selected
				st  	r3, sp, 3	//scratch
				st	r4, sp, 4	   //input mask
				st	r5, sp, 5	  
				st	r6, sp, 6     
				add	sp, sp, 7
				
					
					//Need to add mask decision here
					//needs to find first available cursor location for mask
					mov r1, 0x0000
			GBNextMask:
					and	r0, r4, 0x0002  //will never have cursor at first row
					bc ZS, GBNextMask2
					mov r1, 0x0001
					bra GBMaskFinished
			GBNextMask2: 
					and	r0, r4, 0x0004
					bc ZS, GBNextMask3
					mov r1, 0x0010
					bra GBMaskFinished
			GBNextMask3:
					and	r0, r4, 0x0008
					bc ZS, GBNextMask4
					mov r1, 0x0018
					bra GBMaskFinished
					
			GBNextMask4:
					and	r0, r4, 0x0010
					bc ZS, GBNextMask5
					mov	r1, 0x0020
					bra GBMaskFinished
					
			GBNextMask5:
					and	r0, r4, 0x0020
					bc ZS, GBNextMaskError
					mov 	r1, 0x0028
					bra GBMaskFinished
			
			GBNextMaskError: //User entered invalid mask, assuming row 1
					mov	r1, 0x0008
			GBMaskFinished:
					mov r2, r1
					jsr r6, UpdateCursor
					
					//r5 = current curos location
					mov r5, r2 
	
			GetButtonLoop:	
					jsr 	r6, GetButtonEvent //returns var in r1
					
					and 	r2, r1, 0x0002 		//should be second lsb, or Down
					bc	ZC,	GetButtonDown 		//brancch to down
					
					and	r2, r1, 0x0004		//should be 3rd LSB, or UP
					bc	ZC, GetButtonUp
					
					and	r2,	r1, 0x0001		//LSB, or Centre
					bc	ZC,	GetSelect
					
					//add button mask for BACK to cancel/go back a menu
					and	r2, r1, 0x0080		//TPLeftButton (back)
					bc	ZS, GetButtonLoop		//if not the centre, branch gtfo of here
					mov	r2, 99
					bra EndMenuScroll		//Back was pressed, get return 99 and exit this function
					
			GetButtonUp:
					mov 	r1, r5 		//move cursor location into r1
					add	r2, r1, -8 		//makes NEW POINTER LOCATION 
					bc	ZS, GetButtonLoop 	//if position would be zero we don't want to move up anymore, so go back to getbuttonloop
						
					jsr 	r6, UpdateCursor	//updates cursor, r1 = old loc, r2 = newloc
					jsr 	r6, DelayReallyLong
					jsr 	r6, DelayReallyLong
					mov 	r5, r2	//backs up cursor location to r5
					bra 	GetButtonLoop		//back to ButtonLoop to wait for input
					
			GetButtonDown:
					mov	r1, r5 		//loads current  cursor location into r1
					add	r2, r1, 8 		//NEW POINTER LOCATION = r2
					add	r0, r2, -40
					bc	ZS, GetButtonLoop 	//if position would be 48 we don't want to move down anymore, so go back to getbuttonloop
					
					jsr 	r6, UpdateCursor	//updates cursor, r1 = old loc, r2 = newloc
					jsr 	r6, DelayReallyLong
					jsr 	r6, DelayReallyLong
					mov	r5, r2 	//backs up cursor location to r5
					bra GetButtonLoop		//back to ButtonLoop to wait for input
					
			GetSelect:
					
					//need to figure out which menu option we are on based on curent cursor position, then branch to the appropriate MenuBranch
					mov 	r1,  r5//moves current cursor location to r1
					
					add	r2, r1, -40  //row5 selected
					bc	ZS, Row6Select
					
					add	r2, r1, -32	 //row4 selected
					bc	ZS, Row5Select
					
					add	r2, r1, -24  //row3 selected
					bc	ZS, Row4Select
					
					add	r2, r1, -16  //row2secleted
					bc	ZS, Row3Select	
					
					add	r2, r1, -8	 //row1sleected
					bc	ZS, Row2Select
					
				bra	GetButtonLoop //never will select row0
				
			Row6Select:
					mov 	r2, 6
					bra EndMenuScroll
			Row5Select:
					mov 	r2, 5
					bra EndMenuScroll
			Row4Select:
					mov 	r2, 4
					bra EndMenuScroll
			Row3Select:
					mov 	r2, 3
					bra EndMenuScroll
			Row2Select:
					mov 	r2, 2
					bra EndMenuScroll
					
			EndMenuScroll:
			// Got what we came for, output is stored in R2. Get out of this function with returned value
				sub	sp, sp, 7
				ld	r0, sp, 0	//scratch
				ld	r1, sp, 1	//scratch
				//ld	r2, sp, 2	
				ld  	r3, sp, 3	//scratch
				ld	r4, sp, 4	   //input mask
				ld	r5, sp, 5	  
				ld	r6, sp, 6     
				jsr r6, r6